﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _2.MaximalSum2x2and3x3
{
    class Program
    {
        static void Main(string[] args)
        {
            /*int[,] matrix = {
                            {1, 1, 3, 3, 1, 1, 8, 8},
                            {1, 9, 9, 9, 1, 2, 8, 8},
                            {1, 9, 9, 9, 1, 0, 3, 3},
                            {1, 9, 9, 9, 1, 0, 3, 3},
            };/*/
            Console.Write("n=");
            int rows = int.Parse(Console.ReadLine());
            Console.WriteLine();
            Console.Write("k=");
            int columns = int.Parse(Console.ReadLine());
            Console.WriteLine();
            int[,] matrix = new int[rows, columns];
            String inputNumber;
            for (int row = 0; row < rows; row++)
            {
                for (int column = 0; column < columns; column++)
                {
                    Console.Write("matrix[{0},{1}] = ", row, column);
                    inputNumber = Console.ReadLine();
                    matrix[row, column] = int.Parse(inputNumber);
                }
            }
            int bestSum2x2 = int.MinValue;
            int bestSum3x3 = int.MinValue;
            //bestsum2x2
            for (int row = 0; row < matrix.GetLength(0) - 1; row++)
            {
                for (int col = 0; col < matrix.GetLength(1) - 1; col++)
                {
                    int sum2x2 = matrix[row, col] + matrix[row, col + 1] + matrix[row + 1, col] + matrix[row + 1, col + 1];
                    if (sum2x2 > bestSum2x2)
                    {
                        bestSum2x2 = sum2x2;
                    }
                }
            }
            //bestsum3x3
            for (int row = 0; row < matrix.GetLength(0) - 2; row++)
            {
                for(int col = 0; col<matrix.GetLength(1) - 2;col++)
                {
                    
                    int sum3x3 = matrix[row, col] + matrix[row, col + 1] + matrix[row, col + 2] + matrix[row + 1, col] + matrix[row + 1, col + 1] + matrix[row + 1, col + 2] + matrix[row + 2, col] + matrix[row + 2, col + 1] + matrix[row + 2, col + 2];
                    if (sum3x3 > bestSum3x3)
                    {
                        bestSum3x3 = sum3x3;
                    }
                }
            }
            Console.WriteLine("Bestsum2x2: {0} ",bestSum2x2);
            Console.WriteLine("BestSum3x3: {0} ",bestSum3x3);
        }
    }
}
